var EndLayer = cc.Layer.extend({
      tm :null,
	ctor:function (args) {
		this._super();
		this.tm = args;
		cc.log("end")
		
		//invoke ad
		adNumber = Storage.getAdNumber();
		cc.log(adNumber);
		if(adNumber == 2){
			jsb.reflection.callStaticMethod
			("org/cocos2dx/javascript/AppActivity", "showAd", "()V");
			Storage.setAdNumber(0);
		} else {
			Storage.setAdNumber(++adNumber);
		}
		
		if('touches' in cc.sys.capabilities) {   // 监听返回键，菜单键
			cc.eventManager.addListener({ 
				event: cc.EventListener.KEYBOARD, 
				onKeyReleased: function(keyCode, event) { 
					if (keyCode == cc.KEY.back) {   

//						Storage.setMusicState(music);
//						cc.audioEngine.stopMusic();
//						cc.director.end(); 
					} 
					else if (keyCode == cc.KEY.menu) {    
						
					} 
				}}, this);

			// 进入后台
			cc.eventManager.addCustomListener(cc.game.EVENT_HIDE, function(){ 
				MusicPlayer.pauseResumeMusic(false);
				cc.log("background");
			});
			// 恢复显示
			cc.eventManager.addCustomListener(cc.game.EVENT_SHOW, function(){ 
				
				MusicPlayer.pauseResumeMusic(true);
				cc.log("show" + music);
			});
		}
	},
      onEnter:function(){
    	  this._super();
    	  var menu2;
//    	  if(REAL_WIN_SIZE.height/REAL_WIN_SIZE.width == 3/2)
//    		  menu2 = ccs.load(res.END_640X960_JSON).node;
//    	  else if(REAL_WIN_SIZE.height/REAL_WIN_SIZE.width == 16/9)
//    		  menu2 = ccs.load(res.End_json).node;
//    	  else
//    		  menu2 = ccs.load(res.End_json).node;

    	  background = new cc.Sprite("res/bg.png");
    	  background.x = DESIGN_SIZE.width/2;
    	  background.y = DESIGN_SIZE.height/2;
    	  this.addChild(background);
    	  
    	  scoreText = new ccui.Text("0", "res/DFWaWaW5-GB.ttc", 72, cc.size(220, 72), cc.TEXT_ALIGNMENT_CENTER, cc.VERTICAL_TEXT_ALIGNMENT_TOP);
    	  scoreText.x = 540;
    	  scoreText.y = 1578.82;
    	  scoreText.setTextColor( cc.color( 3, 176, 224));
    	  this.addChild( scoreText);
    	  scoreText.scaleX = 1.82;
    	  scoreText.scaleY = 2;

    	  bestText = new ccui.Text("0", "res/DFWaWaW5-GB.ttc", 72, cc.size(440, 72), cc.TEXT_ALIGNMENT_CENTER, cc.VERTICAL_TEXT_ALIGNMENT_TOP);
    	  bestText.x = 540;
    	  bestText.y = 1408.7;
    	  bestText.setTextColor( cc.color( 3, 176, 224));
    	  this.addChild( bestText);
    	  
    	  scoreText.setString(this.tm+"\"");
    	  this.highscore();
    	  
    	  restart = new ccui.Button("res/Button_Restart_Big.png");
    	  restart.x = 540;
    	  restart.y = 696;
    	  this.addChild(restart);
    	  restart.name = "repeat";
    	  restart.addTouchEventListener(this.buttonTouchEvent);
    	  
    	  share = new ccui.Button("res/Button_Share.png");
    	  share.x = 848.77;
    	  share.y = 1053.12;
    	  this.addChild(share);
    	  share.name = "share";
    	  share.addTouchEventListener(this.buttonTouchEvent);
    	  
    	  back = new ccui.Button("res/Button_Return.png");
    	  back.x = 249.91;
    	  back.y = 1053.12;
    	  this.addChild(back);
    	  back.name = "back";
    	  back.addTouchEventListener(this.buttonTouchEvent);
    	  
    	  musicbut = new ccui.Button("res/Button_Closemusic.png");
    	  musicbut.x = 123.44;
    	  musicbut.y = 1826.69;
    	  this.addChild(musicbut);
    	  musicbut.name = "music";
    	  musicbut.loadTextureDisabled( "res/Button_Upmusic.png", 0);
    	  musicbut.addTouchEventListener(this.buttonTouchEvent);
    	  
    	  if(!music)
    		  musicbut.setBright(false);
    	  
    	  coinSprite = new cc.Sprite("res/CoinBg.png");
    	  coinSprite.x = 929.66;
    	  coinSprite.y = 1839.36;
    	  this.addChild(coinSprite);
    	  
    	  coins = new ccui.Text("0", "res/DFWaWaW5-GB.ttc", 48, cc.size(125, 60), cc.TEXT_ALIGNMENT_LEFT, cc.VERTICAL_TEXT_ALIGNMENT_TOP);
    	  coins.x = 888.52;
    	  coins.y = 1839.94;
    	  coins.setTextColor( cc.color( 255, 165, 0));
    	  this.addChild( coins);

    	  oldcoin = sys.localStorage.getItem(COIN_COUNT_TAG);
    	  if(oldcoin!=''){
    		  coins.setString(oldcoin);
    		  newcoin = parseInt(oldcoin);
    	  }
    	  else{
    		  coins.setString("0");
    		  newcoin = 0;
    	  }
    	  
    	  tips = new ccui.Text("0", "res/DFWaWaW5-GB.ttc", 45, cc.size(1920, 100), cc.TEXT_ALIGNMENT_CENTER, cc.VERTICAL_TEXT_ALIGNMENT_TOP);
    	  tips.x = 540;
    	  tips.y = 384;
    	  this.addChild( tips);
    	  settip();

    	  sea1 = new cc.Sprite("res/hailang3.png");
    	  sea1.x = 540;
    	  sea1.y = 257.66;
    	  cc.log(sea1.width + ":" + sea1.height);
    	  sea1.scaleX = 0.7897;
    	  this.addChild(sea1);
    	  var act1 = cc.moveBy(2, cc.p( 0, 70));
    	  var reverse1 = cc.moveBy(2, cc.p( 0, -70));
    	  sea1.runAction(cc.repeatForever( cc.Sequence( act1, reverse1)));

    	  sea2 = new cc.Sprite("res/hailang 1.png");
    	  sea2.x = 540;
    	  sea2.y = 154.94;
    	  sea2.scaleX = 0.7897;
    	  this.addChild(sea2);
    	  var act2 = cc.moveBy(1.8, cc.p( 0, 50));
    	  var reverse2 = cc.moveBy(1.8, cc.p( 0, -50));
    	  sea2.runAction(cc.repeatForever( cc.Sequence( act2, reverse2)));

    	  sea3 = new cc.Sprite("res/hailang 2.png");
    	  sea3.x = 540;
    	  sea3.y = 45.31;
    	  sea3.scaleX = 0.7897;
    	  this.addChild(sea3);
    	  var act3 = cc.moveBy(1.7, cc.p( 0, 50));
    	  var reverse3 = cc.moveBy(1.7, cc.p( 0, -50));
    	  sea3.runAction(cc.repeatForever( cc.Sequence( act3, reverse3)));

    	  //share txt
    	  toastLayer = new ToastLayer();
    	  toastLayer.addShare();
    	  this.addChild(toastLayer, 10);
    	  toastLayer.setVisible(false);
    	  
    	  return true;
      },
      highscore: function(){
    
    	  if(curGameMode == CLASSIC_MODE){
    		  sc = sys.localStorage.getItem(CLASSIC_BEST_SCORE_TAG);
    	  }else{
    		  sc = sys.localStorage.getItem(PROP_BEST_SCORE_TAG);
    	  }
    	  var now = parseFloat(this.tm);
    	  var old = parseFloat(sc);
    	  if(!(sc=="")){
    		  if(now>old){
    			  if(curGameMode == CLASSIC_MODE){
    				  sys.localStorage.setItem(CLASSIC_BEST_SCORE_TAG ,this.tm);
    			  }else{
    				  sys.localStorage.setItem(PROP_BEST_SCORE_TAG ,this.tm);
    			  }
    			  bestText.setString("Best:"+this.tm+"\"");
    		  }else{
    			  bestText.setString("Best:"+old.toFixed(2)+"\"");
    		  }
    	  }else{
    		  if(curGameMode == CLASSIC_MODE){
    			  sys.localStorage.setItem(CLASSIC_BEST_SCORE_TAG ,this.tm);
    		  }else{
    			  sys.localStorage.setItem(PROP_BEST_SCORE_TAG ,this.tm);
    		  }
    		  bestText.setString("Best:"+now.toFixed(2)+"\"");
    	  }
    	  return "hs";
      },
      // Button控件触发的事件处理
      buttonTouchEvent:function(sender,type){
    	  
    	  switch (type) {
    	  case ccui.Widget.TOUCH_BEGAN:
    		  sender.setScale(1.2, 1.2);
    		  break;
    	  case ccui.Widget.TOUCH_ENDED:
    		  sender.setScale(1, 1);
    		  var scene = new cc.Scene();
    		  switch(sender.getName()){
    		  case "repeat":
    			  
    			  MusicPlayer.playClickBtnEffect();
    			  if(curGameMode == PROP_MODE){
    				  layer = new PropModeScene();
    			  }else{
    				  layer = new ClassicModeScene();
    			  }
    			  break;
    		  case "share":
    			  
    			  MusicPlayer.playClickBtnEffect();
    			  //add the share code.
    			  var result = jsb.reflection.callStaticMethod
    			  ("org/cocos2dx/javascript/AppActivity", "showShare", "(F)V", tm);
    			  break;
    		  case "back":
    			  
    			  MusicPlayer.playClickBtnEffect();
    			  layer = new StartLayer();
    			  break;
    		  case "music":
    			  if(music){
    				  // sender.setTexture(res.music_off);
    				  sender.setBright(false);
    				  cc.audioEngine.pauseMusic();
    			  }else{
    				  if(music)
    					  cc.audioEngine.playEffect(res.buttonClickEffect, false);
    				  // sender.setTexture(res.music_on);
    				  sender.setBright(true);
    				  cc.audioEngine.resumeMusic();
    			  }
    			  music= !music;
    			  break;
    		  }
    		  if(sender.getName() != "music" && sender.getName() != "share" ){
    			  scene.addChild(layer);
	    		  var finalScene = new cc.TransitionCrossFade(0.5,scene);
	    		  cc.director.replaceScene(finalScene);
    		  }
    		  
    		  break;
    	  case ccui.Widget.TOUCH_CANCELED:
    		  sender.setScale(1, 1);
    		  break;
    	  default:
    		  break;
    	  }
      }
});

var settip = function(){
	var t = parseInt(Math.random()*35);
	tips.setString(tipArray[t]);
}

var shareSuccess = function() {
	
	coin = parseInt(coins.getString()) + 20;
	sys.localStorage.setItem(COIN_COUNT_TAG, coin);
	coins.setString(coin);	
	
	toastLayer.setVisible(true);
	toastLayer.showShare();
}

var EndScene = cc.Scene.extend({
	
	ctor:function (args) {
		this._super();
		var layer = new EndLayer(args);
		this.addChild(layer);
		
		return true;
	},
	onEnter:function () {
		this._super();
		
	}
});



